/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortus.util;

import java.util.HashMap;
import java.util.List;
import sagex.api.Global;
import sagex.api.Configuration;
import sagex.api.MediaFileAPI;
/**
 *
 * @author Aaron
 */
public class ui {
	/*
	 * Given a position relative to the full visible screen will return the position taking into account overscan.
	 * Good for "Background Components" that need to line up with non background panel widgets.
	 * @param A - the UI position (0.00 - 1.00)
	 * @param wrt0 - true = A is wrt 0 (top), false = A is wrt 1 (bottom)
	 */
	public static double GetVerticalPositionWithOverscan(double A, boolean wrt0)
	{
		double B = 0;

		if (A < 0) {
			A = 0;
		}
		if (A > 1.0) {
			A = 1.0;
		}

		double Dim = Global.GetFullUIHeight(ortus.api.GetUIContext());
		double Offset = Configuration.GetOverscanOffsetY(ortus.api.GetUIContext());
		double Scale = Configuration.GetOverscanScaleHeight(ortus.api.GetUIContext());

		if (wrt0) {
			B = Offset / Dim + A * Scale;
		} else {
			B = 1 - (Offset / Dim + (1 - A) * Scale);
		}

		if (B < 0) {
			B = 0;
		}
		if (B > 1.0) {
			B = 1.0;
		}

//		ortus.api.DebugLog(TRACE, "Vertical Position With Overscan");
//		ortus.api.DebugLog(TRACE, "Input = '" + A + "'");
//		ortus.api.DebugLog(TRACE, "FullUIWidth = '" + Dim + "'");
//		ortus.api.DebugLog(TRACE, "OverscanOffsetX = '" + Offset + "'");
//		ortus.api.DebugLog(TRACE, "OverscanScaleWidth = '" + Scale + "'");
//		ortus.api.DebugLog(TRACE, "Output = '" + B + "'");

		return B;
	}

	/*
	 * Given a relative position will return the true position taking into account overscan.
	 * Good for "Background Components" that need to line up with non background panel widgets.
	 * @param A - the UI position (0.00 - 1.00)
	 * @param wrt0 - true = A is wrt 0 (left), false = A is wrt 1 (right)
	 */
	public static double GetHorizontalPositionWithOverscan(double A, boolean wrt0)
	{
		double B = 0;

		if (A < 0) {
			A = 0;
		}
		if (A > 1.0) {
			A = 1.0;
		}

		double Dim = Global.GetFullUIWidth(ortus.api.GetUIContext());
		double Offset = Configuration.GetOverscanOffsetX(ortus.api.GetUIContext());
		double Scale = Configuration.GetOverscanScaleWidth(ortus.api.GetUIContext());

		if (wrt0) {
			B = Offset / Dim + A * Scale;
		} else {
			B = 1 - (Offset / Dim + (1 - A) * Scale);
		}

		if (B < 0) {
			B = 0;
		}
		if (B > 1.0) {
			B = 1.0;
		}

//		ortus.api.DebugLog(TRACE, "Horizontal Pos With Overscan");
//		ortus.api.DebugLog(TRACE, "Input = '" + A + "'");
//		ortus.api.DebugLog(TRACE, "FullUIWidth = '" + Dim + "'");
//		ortus.api.DebugLog(TRACE, "OverscanOffsetX = '" + Offset + "'");
//		ortus.api.DebugLog(TRACE, "OverscanScaleWidth = '" + Scale + "'");
//		ortus.api.DebugLog(TRACE, "Output = '" + B + "'");
		return B;
	}

	public static Object GetMediaObject(Object o1, Object o2) {
		if (MediaFileAPI.IsMediaFileObject(o1))
			return o1;

		if ( o1 instanceof String) {
			return ((List)((HashMap)o2).get(o1)).get(0);
		} else
			return null;
	}
}
